package cn.mw.cmdb.service.impl;

import cn.mw.cmdb.entity.ItemNameMapperInfo;
import cn.mw.cmdb.enums.MonitoringServerType;
import cn.mw.cmdb.mongoMapper.ItemNameMapper;
import cn.mw.cmdb.processor.MonitorParamContext;
import cn.mw.cmdb.processor.MonitorProcessor;
import cn.mw.cmdb.service.MwMonitorProcessorConvert;
import cn.mw.main.api.common.ZabbixDubboService;
import com.google.common.base.Strings;

import java.util.ArrayList;
import java.util.List;

public class ItemNameMapperServiceImpl extends ModelBaseServiceImpl<ItemNameMapperInfo, Object> {

    private CmdbServiceManage cmdbServiceManage;
    private ItemNameMapper itemNameMapper;

    private ZabbixDubboService zabbixDubboService;


    public static final String ID = "itemNameMapper";

    public ItemNameMapperServiceImpl(ItemNameMapper itemNameMapper, CmdbServiceManage cmdbServiceManage, ZabbixDubboService zabbixDubboService) {
        this.itemNameMapper = itemNameMapper;
        this.cmdbServiceManage = cmdbServiceManage;
        this.zabbixDubboService = zabbixDubboService;
    }

    @Override
    public ItemNameMapperInfo genObject() {
        return new ItemNameMapperInfo();
    }

    @Override
    Object doAdd(ItemNameMapperInfo param) throws Exception {

        return null;
    }

    @Override
    boolean doUpdate(ItemNameMapperInfo param) throws Exception {
        MwMonitorProcessorConvert processorConvert = new MwMonitorProcessorConvert(cmdbServiceManage, zabbixDubboService);
        MonitorParamContext contextParam = processorConvert.getContextParam(param, itemNameMapper);
        MonitorProcessor processor = null;
        if(Strings.isNullOrEmpty(param.getInstanceId())){
            String type = param.getType();
            if(Strings.isNullOrEmpty(type)){
                //默认查询zabbix监控服务
                type = MonitoringServerType.Zabbix.name();
            }
            MonitoringServerType monitoringServerType = MonitoringServerType.valueOf(type);
             processor = processorConvert.getMonitorProcessor(monitoringServerType);
        }else{
             processor =  processorConvert.getMonitorProcessor(param.getInstanceId());
        }
        if (processor != null) {
            processor.addAndUpdateItemNameMapper(contextParam);
        }
        return true;
    }

    @Override
    boolean doRemove(ItemNameMapperInfo param) throws Exception {
        return false;
    }

    @Override
    Object doBatchAdd(Object params) throws Exception {
        return false;
    }

    @Override
    boolean doBatchUpdate(Object params) throws Exception {
        return false;
    }

    @Override
    ItemNameMapperInfo doSelectById(Object id) throws Exception {
        return null;
    }

    @Override
    List<ItemNameMapperInfo> doSelectList(Object obj) throws Exception {
        ItemNameMapperInfo param = new ItemNameMapperInfo();
        if (obj instanceof ItemNameMapperInfo) {
            param = (ItemNameMapperInfo) obj;
        }
        MwMonitorProcessorConvert processorConvert = new MwMonitorProcessorConvert();
        MonitoringServerType type = MonitoringServerType.valueOf(param.getType());
        //获取查询监控processor
        MonitorProcessor processor = processorConvert.getMonitorProcessor(type);
        List<ItemNameMapperInfo> itemNameMapperList = new ArrayList<>();
        if (processor != null) {
            itemNameMapperList = processor.getItemNameMapperList(param);
            for (ItemNameMapperInfo itemInfo : itemNameMapperList) {
                //忽略null值注入
                itemInfo.setItem(itemInfo.getZabbixItem());
            }
        }
        return itemNameMapperList;
    }

    @Override
    boolean doBatchRemove(Object params) throws Exception {
        return false;
    }

}
